package com.tianqi.modules.tqs.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianqi.common.exception.ApiException;
import com.tianqi.modules.tqs.dto.CommonExcelData;
import com.tianqi.modules.tqs.enums.FileExtensionEnum;
import com.tianqi.modules.tqs.excel.CommonExcelListener;
import com.tianqi.modules.tqs.mapper.TqCoreDataMapper;
import com.tianqi.modules.tqs.model.TqCoreData;
import com.tianqi.modules.tqs.service.TqCoreDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rory
 * @since 2023-08-05
 */
@Service
@Slf4j
public class TqCoreDataServiceImpl extends ServiceImpl<TqCoreDataMapper, TqCoreData> implements TqCoreDataService {

	@Autowired
	private TqCoreDataMapper tqCoreDataMapper;

	/**
	 * 上传Excel解析为业务数据
	 *
	 * @param file
	 * @return
	 */
	@Override
	public Boolean uploadExcelData(MultipartFile file) {
		//校验文件后缀
		String extension =
				Objects.requireNonNull(FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase();
		if (!Objects.equals(extension, FileExtensionEnum.XLSX.getName())) {
			throw new ApiException("文件类型错误");
		}
		try {
			InputStream inputStream = file.getInputStream();
			ExcelReaderBuilder readerBuilder = EasyExcel.read(inputStream, CommonExcelData.class,
					new CommonExcelListener(tqCoreDataMapper));
			readerBuilder.sheet().doReadSync();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return true;
	}

	/**
	 * 根据两个条件查询数据
	 *
	 * @param type
	 * @param param1
	 * @param param2
	 * @return
	 */
	@Override
	public TqCoreData selectValueByCondition(String type, String param1, String param2) {
		LambdaQueryWrapper<TqCoreData> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TqCoreData::getType, type);
		queryWrapper.eq(TqCoreData::getConditionOne, param1);
		queryWrapper.eq(TqCoreData::getConditionTwo, param2);
		TqCoreData tqCoreData = tqCoreDataMapper.selectOne(queryWrapper);
		// 如果查询结果为空，则交换条件
		if (Objects.isNull(tqCoreData)) {
			LambdaQueryWrapper<TqCoreData> queryWrapper2 = new LambdaQueryWrapper<>();
			queryWrapper2.eq(TqCoreData::getType, type);
			queryWrapper2.eq(TqCoreData::getConditionOne, param2);
			queryWrapper2.eq(TqCoreData::getConditionTwo, param1);
			tqCoreData = tqCoreDataMapper.selectOne(queryWrapper2);
			if(Objects.isNull(tqCoreData)){
				tqCoreData=new TqCoreData();
				tqCoreData.setValue("");
			}
		}
		return tqCoreData;
	}
}
